/**
 * 
 */
package com.example.radioclub.login.yahoo;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

/**
 * OAuth helper for Yahoo.
 * 
 * @author wizard
 *
 */
public class YahooOAuthHelper {
	private OAuthConsumer consumer;
	private OAuthProvider provider;
	private String callbackUrl, consumerKey, consumerSecret;
	
	public YahooOAuthHelper(String consumerKey, String consumerSecret, String callback) {
		// TODO Auto-generated constructor stub
		this.consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
		this.provider = new CommonsHttpOAuthProvider(
				"https://api.login.yahoo.com/oauth/v2/get_request_token", 	// request token 
				"https://api.login.yahoo.com/oauth/v2/get_token", 			// access token
				"https://api.login.yahoo.com/oauth/v2/request_auth");		// auth token
		this.provider.setOAuth10a(true);
		this.callbackUrl = (callback == null ? OAuth.OUT_OF_BAND : callback);
		this.consumerKey = consumerKey;
		this.consumerSecret = consumerSecret;
	}
	
	public String getRequestToken(){
		try {
			return this.provider.retrieveRequestToken(this.consumer, this.callbackUrl);
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String[] getAccessToken(String verifier) {
		try {
			this.provider.retrieveAccessToken(this.consumer, verifier);
			return new String[] { this.consumer.getToken(), this.consumer.getTokenSecret() };
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
